package com.baoyi.ad_client.ad;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;

public class AdManager {

	public static String Anzhi_Feiwo_URL = "http://ad.xabaoyi.com:82/Ad_Web/servlet/Anzhi_Feiwo_CheckVersionCode";
	public static String Vgao_Anzhi_URL = "http://ad.xabaoyi.com:82/Ad_Web/servlet/Vgao_Anzhi_CheckVersionCode";
	public static String Vgao_Baidu_URL = "http://ad.xabaoyi.com:82/Ad_Web/servlet/Vgao_Baidu_CheckVersionCode";
	public static String DUOMENG_URL = "http://ad.xabaoyi.com:82/Ad_Web/servlet/DuoMeng_CheckVersionCode";
	public static String DIANJIN_BANNER_URL = "http://ad.xabaoyi.com:82/Ad_Web/servlet/Dianjin_Banner_CheckVersionCode";
	public static String DIANJIN_CP_URL = "http://ad.xabaoyi.com:82/Ad_Web/servlet/Dianjin_CP_CheckVersionCode";
	public static String DYD_TS_URL = "http://ad.xabaoyi.com:82/Ad_Web/servlet/DYD_TS_CheckVersionCode";
	private AdReciver adReciver;
	private AdReciver chaPinReciver;
	private AdReciver pushReciver;

	public AdReciver getChaPinReciver() {
		return chaPinReciver;
	}

	public void setChaPinReciver(AdReciver chaPinReciver) {
		this.chaPinReciver = chaPinReciver;
	}

	public AdReciver getPushReciver() {
		return pushReciver;
	}

	public void setPushReciver(AdReciver pushReciver) {
		this.pushReciver = pushReciver;
	}

	public AdReciver getAdReciver() {
		return adReciver;
	}

	public void setAdReciver(AdReciver adReciver) {
		this.adReciver = adReciver;
	}

	public void getVg(Context context){
		new getCP().execute(context);
		
	}
	// 获取 点金banner
	public class getBanner extends AsyncTask<Activity, Void, String> {

		Activity activity = null;

		@Override
		protected String doInBackground(Activity... params) {
			// TODO Auto-generated method stub
			activity = params[0];
			return AdManager.UpdateVersionCode(params[0], DIANJIN_BANNER_URL);
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			int isOpen = -1;
			String ad_id_1 = "";
			String ad_id_2 = "";
			try {
				JSONObject jsonObject = new JSONObject(result)
						.getJSONObject("adbean");
				isOpen = jsonObject.getInt("isopen");
				ad_id_1 = jsonObject.getString("ad_id_1").toString();
				ad_id_2 = jsonObject.getString("ad_id_2").toString();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (isOpen == 1) {
				if (adReciver != null) {
					adReciver.setAd(activity,ad_id_1, ad_id_2, isOpen);
				}
			}
		}
	}
	// 获取 安智banner
	public class getAnzhiBanner extends AsyncTask<Activity, Void, String> {
		Activity activity = null;
		@Override
		protected String doInBackground(Activity... params) {
			activity = params[0];
			return AdManager.UpdateVersionCode(params[0], Anzhi_Feiwo_URL);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			int isOpen = -1;
			String ad_id_1 = "";
			String ad_id_2 = "";
			try {
				JSONObject jsonObject = new JSONObject(result)
						.getJSONObject("adbean");
				isOpen = jsonObject.getInt("isopen");
				ad_id_1 = jsonObject.getString("ad_id_1").toString();
				ad_id_2 = jsonObject.getString("ad_id_2").toString();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (isOpen == 1) {
				if (adReciver != null) {
					adReciver.setAd(activity,ad_id_1, ad_id_2, isOpen);
				}
			}
		}
	}

	// 获取插屏
	public class getCP extends AsyncTask<Context, Void, String> {

		Context activity = null;

		@Override
		protected String doInBackground(Context... params) {
			activity = params[0];
			return AdManager.UpdateVersionCode(params[0], DYD_TS_URL);
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			int isOpen = -1;
			String ad_id_1 = "";
			String ad_id_2 = "";
			try {
				JSONObject jsonObject = new JSONObject(result)
						.getJSONObject("adbean");
				isOpen = jsonObject.getInt("isopen");
				ad_id_1 = jsonObject.getString("ad_id_1").toString();
				ad_id_2 = jsonObject.getString("ad_id_2").toString();
			} catch (JSONException e) {
				e.printStackTrace();
			}
			if (isOpen == 1) {
				if (pushReciver != null) {
					pushReciver.setAd(activity,ad_id_1, ad_id_2, isOpen);
				}
				if (chaPinReciver != null) {
					chaPinReciver.setAd(activity,ad_id_1, ad_id_2, isOpen);
				}

			}
		}
	}
	// 获取插屏
	public class getVG extends AsyncTask<Context, Void, String> {

		Context activity = null;

		@Override
		protected String doInBackground(Context... params) {
			activity = params[0];
			return AdManager.UpdateVersionCode(params[0], Vgao_Anzhi_URL);
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			int isOpen = -1;
			String ad_id_1 = "";
			String ad_id_2 = "";
			try {
				JSONObject jsonObject = new JSONObject(result)
						.getJSONObject("adbean");
				isOpen = jsonObject.getInt("isopen");
				ad_id_1 = jsonObject.getString("ad_id_1").toString();
				ad_id_2 = jsonObject.getString("ad_id_2").toString();
			} catch (JSONException e) {
				e.printStackTrace();
			}
			if (isOpen == 1) {
				if (pushReciver != null) {
					pushReciver.setAd(activity,ad_id_1, ad_id_2, isOpen);
				}
				if (chaPinReciver != null) {
					chaPinReciver.setAd(activity,ad_id_1, ad_id_2, isOpen);
				}

			}
		}
	}

	public static String UpdateVersionCode(Context activity, String URL) {
		String result = "";
		try {
			HttpClient httpClient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost();
			httpPost.setURI(new URI(URL));
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("pname", AdManager
					.getAppPackageName(activity)));
			params.add(new BasicNameValuePair("vcode", ""
					+ AdManager.getAppVersionCode(activity)));
			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params,
					HTTP.UTF_8);
			httpPost.setEntity(formEntity);
			HttpResponse response = httpClient.execute(httpPost);
			System.out.println("返回响应的状态码"
					+ response.getStatusLine().getStatusCode());
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				result = EntityUtils.toString(entity, HTTP.UTF_8);
				System.out.println(result);
				return result;
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	// 获取应用包名
	public static String getAppPackageName(Context activity) {

		PackageInfo packageInfo = null;
		try {
			packageInfo = activity.getPackageManager().getPackageInfo(
					activity.getPackageName(), 0);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String packageName = packageInfo.packageName;

		return packageName;
	}

	// 获取应用版本号
	public static int getAppVersionCode(Context activity) {
		PackageInfo packageInfo = null;
		try {
			packageInfo = activity.getPackageManager().getPackageInfo(
					activity.getPackageName(), 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		int versionCode = packageInfo.versionCode;

		return versionCode;
	}
}
